package com.dream.testtubebaby.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dream.testtubebaby.R;
import com.dream.testtubebaby.activity.BbsListItemDetailActivity;
import com.dream.testtubebaby.data.bbs.BbsNode;
import com.dream.testtubebaby.data.bbs.HotNode;

public class BbsAllBaseAdapter extends BbsBaseAdapter {
	public BbsAllBaseAdapter(Context context, List<BbsNode> items) {
		super(context, items);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater layoutInflater = LayoutInflater.from(context);
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(
					R.layout.bbs_all_list_item_layout, null);
			viewHolder.bbs_item_title_tv = (TextView) convertView
					.findViewById(R.id.bbs_item_title_tv);
			viewHolder.bbs_item_name_tv = (TextView) convertView
					.findViewById(R.id.bbs_item_name_tv);
			viewHolder.bbs_item_lookup_tv = (TextView) convertView
					.findViewById(R.id.bbs_item_lookup_tv);
			viewHolder.bbs_item_comment_tv = (TextView) convertView
					.findViewById(R.id.bbs_item_comment_tv);
			viewHolder.bbs_item_time_tv = (TextView) convertView
					.findViewById(R.id.bbs_item_time_tv);
			viewHolder.bbs_top_hot_view = (LinearLayout) convertView
					.findViewById(R.id.bbs_all_tophot_view);
			viewHolder.bbs_top1_tv = (TextView) convertView
					.findViewById(R.id.bbs_top1_tv);
			viewHolder.bbs_top2_tv = (TextView) convertView
					.findViewById(R.id.bbs_top2_tv);
			viewHolder.bbs_top3_tv = (TextView) convertView
					.findViewById(R.id.bbs_top3_tv);
			viewHolder.bbs_top1_l = (LinearLayout) convertView
					.findViewById(R.id.bbs_top1_l);
			viewHolder.bbs_top2_l = (LinearLayout) convertView
					.findViewById(R.id.bbs_top2_l);
			viewHolder.bbs_top3_l = (LinearLayout) convertView
					.findViewById(R.id.bbs_top3_l);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (position == 0) {
			setBbsHotView(position, viewHolder);
		} else {
			viewHolder.bbs_top_hot_view.setVisibility(View.GONE);
		}

		viewHolder.bbs_item_title_tv.setText(mTextUtil.replace(mDatas.get(position).getTitle()));
		viewHolder.bbs_item_name_tv.setText(mDatas.get(position)
				.getPostUserName());
		viewHolder.bbs_item_lookup_tv.setText(String.valueOf(mDatas.get(
				position).getViewCount())
				+ context.getResources().getString(R.string.bbs_lookup_string));
		viewHolder.bbs_item_comment_tv
				.setText(String.valueOf(mDatas.get(position).getReplyCount())
						+ context.getResources().getString(
								R.string.bbs_comment_string));
		CharSequence time_string = DateUtils.getRelativeTimeSpanString(mDatas
				.get(position).getPostDate(), System.currentTimeMillis(),
				DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);
		viewHolder.bbs_item_time_tv.setText(time_string);
		return convertView;
	}

	private void setBbsHotView(int position, ViewHolder viewHolder) {
		viewHolder.bbs_top_hot_view.setVisibility(View.VISIBLE);
		List<HotNode> hotNodes = mDatas.get(position).getHotNodes();
		for (int i = 0; i < hotNodes.size(); i++) {
			String title = hotNodes.get(i).getTitle();
			if (i == 0) {
				viewHolder.bbs_top1_tv.setText(mTextUtil.replace(title));
				viewHolder.bbs_top1_l.setTag(hotNodes.get(i).getId());
				viewHolder.bbs_top1_l.setOnClickListener(mListener);
			} else if (i == 1) {
				viewHolder.bbs_top2_tv.setText(mTextUtil.replace(title));
				viewHolder.bbs_top2_l.setTag(hotNodes.get(i).getId());
				viewHolder.bbs_top2_l.setOnClickListener(mListener);
			} else if (i == 2) {
				viewHolder.bbs_top3_tv.setText(mTextUtil.replace(title));
				viewHolder.bbs_top3_l.setTag(hotNodes.get(i).getId());
				viewHolder.bbs_top3_l.setOnClickListener(mListener);
			}
		}
		if(hotNodes.size() == 0){
			viewHolder.bbs_top_hot_view.setVisibility(View.GONE);
		}
	}

	OnClickListener mListener = new OnClickListener() {
		public void onClick(View v) {
			try {
				Intent intent = new Intent(context,
						BbsListItemDetailActivity.class);
				Integer id = (Integer)(v.getTag());
				int _id = id.intValue();
				intent.putExtra(BbsListItemDetailActivity._ID, _id);
				context.startActivity(intent);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	
	class ViewHolder {
		TextView bbs_item_title_tv;
		TextView bbs_item_name_tv;
		TextView bbs_item_lookup_tv;
		TextView bbs_item_comment_tv;
		TextView bbs_item_time_tv;
		LinearLayout bbs_top_hot_view;
		TextView bbs_top1_tv;
		LinearLayout bbs_top1_l;
		TextView bbs_top2_tv;
		LinearLayout bbs_top2_l;
		TextView bbs_top3_tv;
		LinearLayout bbs_top3_l;
	}
}
